home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* scene.c - open a window and clear the background.
- * Set up callbacks to handle keyboard input.
- * Model some objects. Use perspective projection.
- * Use independent modeling transformations to position objects.
- *
- * F1 key - print help information
- * SPACE key - generates a random background color
- * <s> key - toggle smooth/flat shading
- * Escape Key - exit program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <stdio.h>
- #include <math.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
- GLvoid drawScene( GLvoid );
-
- void checkError( char * );
- void printHelp( char * );
-
- /* Global Variables */
-
- static char *progname;
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- /* create a window that is 1/4 the size of the screen,
- * and position it in the middle of the screen.
- */
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( width / 2, height / 2 );
- glutInitDisplayMode( GLUT_RGBA );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutDisplayFunc( drawScene );
-
- progname = argv[0];
-
- printHelp( progname );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout,
- "\n%s - create a scene using independent models\n\n"
- "F1 key - print help information\n"
- "SPACE Key - generates a random background color\n"
- "<s> key - toggle smooth/flat shading\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- /* set clear color to blue */
- glClearColor( 0.0, 0.0, 1.0, 1.0 );
-
- glMatrixMode( GL_PROJECTION );
- gluPerspective( 45.0, 1.0, 3.0, 7.0 );
- glMatrixMode( GL_MODELVIEW );
- }
-
- void
- checkError( char *label )
- {
- GLenum error;
- while ( (error = glGetError()) != GL_NO_ERROR )
- printf( "%s: %s\n", label, gluErrorString(error) );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- static GLboolean flat = GL_FALSE;
-
- switch (key) {
- case ' ': /* SPACE key */
- /* generate a random background color */
- glClearColor( drand48(), drand48(), drand48(), 1.0 );
- glutPostRedisplay();
- break;
- case 's': /* s key */
- /* toggle between smooth and flat shading */
- flat = !flat;
- if (flat)
- glShadeModel( GL_FLAT );
- else
- glShadeModel( GL_SMOOTH );
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_F1: /* Function key #1 */
- /* print help information */
- printHelp( progname );
- break;
- }
- }
-
- GLvoid
- drawWindow()
- {
- /* window */
- static GLfloat v0[] = { 0.0, 0.4 };
- static GLfloat v1[] = { 0.0, 0.0 };
- static GLfloat v2[] = { 0.1, 0.4 };
- static GLfloat v3[] = { 0.1, 0.0 };
- static GLfloat v4[] = { 0.2, 0.4 };
- static GLfloat v5[] = { 0.2, 0.0 };
-
- /* draw 2 quadrilateral strip to make a window */
- glBegin( GL_QUAD_STRIP );
- glColor3f( 1.0, 0.0, 0.0 );
- glVertex2fv (v0);
- glColor3f( 0.9, 0.0, 1.0 );
- glVertex2fv (v1);
- glColor3f( 0.8, 0.1, 0.0 );
- glVertex2fv (v2);
- glColor3f( 0.7, 0.2, 1.0 );
- glVertex2fv (v3);
- glColor3f( 0.6, 0.3, 0.0 );
- glVertex2fv (v4);
- glColor3f( 0.5, 0.4, 1.0 );
- glVertex2fv (v5);
- glColor3f( 0.4, 0.5, 0.0 );
- glEnd();
- }
-
- /* draw a "circle" using a triangle fan; set the
- * center of the circle to white, and the outside
- * to the color passed in
- */
- GLvoid
- drawFan( GLfloat *color )
- {
- /* Draw a triangle fan centered a the current coordinate
- * system origin
- */
- glBegin( GL_TRIANGLE_FAN );
- glColor3f( 1.0, 1.0, 1.0 );
- glVertex2f( 0.0, 0.0 );
- glColor3fv( color );
- glVertex2f( 0.0, -0.2 );
- glVertex2f( 0.2, -0.1 );
- glVertex2f( 0.2, 0.1 );
- glVertex2f( 0.0, 0.2 );
- glVertex2f( -0.2, 0.1 );
- glVertex2f( -0.2, -0.1 );
- glVertex2f( 0.0, -0.2 );
- glEnd();
- }
-
- /* draw a flower with the base of the stem
- * at the current location
- */
- GLvoid
- drawFlower( GLfloat *color )
- {
- static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
- /* draw the stem with 2 leaves */
-
- glColor3fv( darkgreen );
- glBegin( GL_LINES );
- glVertex2f( 0.0, 0.0 ); /* stem */
- glVertex2f( 0.0, 0.25 );
- glVertex2f( 0.0, 0.1 ); /* leaf */
- glVertex2f( 0.05, 0.15 );
- glVertex2f( 0.0, 0.05 ); /* leaf */
- glVertex2f( -0.05, 0.2 );
- glEnd();
-
- glPushMatrix();
- /* move to the top of the stem */
- glTranslatef( 0.0, 0.25, 0.0 );
-
- /* use a scaled triangle fan for the flower head */
- glScalef( 0.1, 0.1, 1.0 );
- drawFan( color );
- glPopMatrix();
- }
-
- /* draw a house centered at the current origin;
- * the bounding box for the house is 1.0 x 2.0 units
- */
- GLvoid
- drawHouse()
- {
- /* draw the house */
- glColor3f( 1.0, 1.0, 1.0 ); /* white */
- glRectf( -0.5, -0.75, 0.5, 0.75 );
-
- /* draw the door */
- glColor3f( 0.5, 0.2, 0.1 ); /* brown */
- glRectf( -0.2, -0.75, 0.2, 0.0 );
-
- glPushMatrix();
- /* move to the top left corner of the house */
- glTranslatef( -0.5, 0.75, 0.0 );
-
- /* draw a triangle for the roof */
- glColor3f( 0.0, 0.0, 0.0 ); /* black */
- glBegin( GL_TRIANGLES );
- glVertex2f( 0.0, 0.0 );
- glVertex2f( 1.0, 0.0 );
- glVertex2f( 0.5, 0.5 );
- glEnd();
- glPopMatrix();
-
- glPushMatrix();
- /* move to the location for the left window */
- glTranslatef( -0.4, 0.2, 0.0 );
- drawWindow();
- glPopMatrix();
-
- glPushMatrix();
- /* move to the location for the right window */
- glTranslatef( 0.2, 0.2, 0.0 );
- drawWindow();
- glPopMatrix();
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static GLfloat red[] = { 1.0, 0.0, 0.0 };
- static GLfloat magenta[] = { 1.0, 0.0, 1.0 };
- static GLfloat yellow[] = { 1.0, 1.0, 0.0 };
- static GLfloat blue[] = { 0.0, 0.0, 1.0 };
- static GLfloat green[] = { 0.0, 1.0, 0.0 };
- static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glPushMatrix();
-
- /* move inside the viewing volume */
- glTranslatef( 0.0, 0.0, -4.0 );
-
- /* draw the ground in different shades of green */
- glBegin( GL_POLYGON );
- glColor3fv( green );
- glVertex2f( -2.0, -2.0 );
- glVertex2f( 2.0, -2.0 );
- glColor3fv( darkgreen );
- glVertex2f( 2.0, 0.0 );
- glVertex2f( -2.0, 0.0 );
- glEnd();
-
- /* draw the house */
- glPushMatrix();
- /* move to where the center of the house should be */
- glTranslatef( -0.5, 0.0, 0.0 );
-
- /* shrink the house slightly */
- glScalef( 0.7, 0.7, 0.7 );
- drawHouse();
- glPopMatrix();
-
- /* draw the sun */
- glPushMatrix();
- /* move to the location of the sun */
- glTranslatef( 2.0, 2.0, -1.5 );
- glColor3fv( yellow );
- glutSolidSphere( 0.2, 8, 15 );
- glPopMatrix();
-
- /* draw several tulips in the foreground */
- glPushMatrix();
- glTranslatef( 1.0, -1.0, 1.0 );
- drawFlower( red );
- glTranslatef( 0.0, 0.0, -0.5 );
- drawFlower( yellow );
- glPopMatrix();
- glPushMatrix();
- glTranslatef( -1.0, -1.0, 1.0 );
- drawFlower( blue );
- glTranslatef( 0.0, 0.0, -0.5 );
- drawFlower( magenta );
- glPopMatrix();
-
- glPopMatrix();
-
- checkError( "drawScene" );
- glFlush();
- }
-